<?php 
	require("libraries/SampleEntryLib.php");
	session_start();

		$numberofsamples = $_POST['numberofsamples'];
		$analyst =  $_POST['analyst'];
		$sampling_date =  $_POST['sampling_date_year']."-".$_POST['sampling_date_month']."-".$_POST['sampling_date_day'];
		$sample_type =  $_POST['sample_type'];
		if(isset($_POST['forall'])){
			$forall = $_POST['forall'];
		}else{
			$forall = 0;
		}
		
		
		$casl_no_year =  $_POST['casl_no_year'];
		$casl_no_sample =  $_POST['casl_no_sample'];
		$sample_category =  $_POST['sample_category'];
		$sample_code =  $_POST['sample_code'];
		$analysis_required =  $_POST['analysis_required'];
		$remarks =  $_POST['remarks'];
		
		$cash = $_POST['cash'];
		$check = $_POST['check'];
		$bank = $_POST['bank'];
		$official_receipt_num = $_POST['official_receipt_num'];
	
	//if(isset($cash))
	//if($cash==NULL)
	
	
	for($i = 0; $i < $numberofsamples; $i++){
		$insert_casl_no_year =  " "; 
		$insert_casl_no_sample =  " ";
		$insert_sample_category  = " ";
		$insert_sample_code  = " ";
		$insert_analysis_required  = " ";
		$insert_remarks = " ";
		
		if($forall){
			
			if(isset($casl_no_year[$i]))
			$insert_casl_no_year =  $casl_no_year[0]; 
			
			if(isset($casl_no_sample[$i]))
			$insert_casl_no_sample =  $casl_no_sample[$i];
			
			if(isset($sample_category[$i]))
			$insert_sample_category =  $sample_category[0];
			
			if(isset($sample_code[$i]))
			$insert_sample_code =  $sample_code[0];
			
			if(isset($analysis_required[$i]))
			$insert_analysis_required =  $analysis_required[0];
			
			if(isset($insert_remarks))
			$insert_remarks =  $remarks[0]; 
		}else{
			if(isset($casl_no_year[$i]))
			$insert_casl_no_year =  $casl_no_year[$i]; 
			
			if(isset($casl_no_sample[$i]))
			$insert_casl_no_sample =  $casl_no_sample[$i];
			
			if(isset($sample_category[$i]))
			$insert_sample_category =  $sample_category[$i];
			
			if(isset($sample_code[$i]))
			$insert_sample_code =  $sample_code[$i];
			
			if(isset($analysis_required[$i]))
			$insert_analysis_required =  $analysis_required[$i];
			
			if(isset($insert_remarks))
			$insert_remarks =  $remarks[$i]; 
		}
		
		echo "------</br>";
		echo "$insert_casl_no_year-$insert_casl_no_sample</br>";
		echo "$analyst</br>";
		echo "$sampling_date</br>";
		echo "$sample_type</br>";
		echo "$insert_sample_category</br>";
		echo "$insert_sample_code</br>";
		echo "$insert_analysis_required</br>";
		echo "$insert_remarks</br>";
		
		$getValues = strtok($insert_analysis_required,'/');
		$insert_analysis_required = $getValues;
		$getValues = strtok('/');
		$insert_main_type = $getValues;
		$getValues = strtok('/');
		$insert_sub_type = $getValues;
	
		
		//must get sample price from database;
		echo $insert_main_type;
		echo $insert_sub_type;
		echo $insert_analysis_required;

		//search for the analysis_required
		$cost = getCost($insert_main_type, $insert_sub_type, $insert_analysis_required, 'casl', 'localhost', 'editcost', 'editcost');
		echo $cost;
		$create = insertSampleParameters($insert_casl_no_year, $insert_casl_no_sample, $insert_main_type, $insert_sub_type, $insert_analysis_required, 'casl', 'localhost', 'login', 'login');
		$update = UpdateSampleEntry($insert_casl_no_year, $insert_casl_no_sample, $sampling_date,
		 $sample_type, $insert_sample_category, $insert_sample_code, $insert_main_type, $insert_sub_type,
		 $insert_analysis_required, $cost, $analyst,  $insert_remarks, $_SESSION['sample_key'],
		 'casl', 'localhost', 'editcost', 'editcost','sample_entry');
	}
	if($update == true){
		if($_POST['submit'] == 'summary')
			header("location:ViewSummary.php?sample_key=".$_SESSION['sample_key']);
		else
			header("location:AdministratorHomepage.php");
	
	}else{
		//header("location:error.php");
	}
	
	
?>